<?php
namespace Core;
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
** etc:  mysql操作类
** author: deepe.xiao
** update: 2018-10-10
*/



class Mysql{
	var $link;        //数据库资源
	var $host;
	var $port;        //数据库端口
	var $user;
	var $passwd;
	var $database;
	static $mysql;
	static $_instance = array();

	function __construct(){
		$config = include_once( FCPATH."/app/config/db.php" );

		if((!$config['host']) || (!$config['port']) || (!$config['user']) || (!$config['passwd']) || (!$config['database'])) {
			view_log(
			   'Db Config Error',
			   'Please check your config array , make sure array include these config item:
			   <font color="red">
			   <br/>$config["host"]
			   <br/>$config["port"]
			   <br/>$config["user"]
			   <br/>$config["passwd"]
			   <br/>$config["database"]
			   </font>
			   '
			);
		}
		
		$this->host = $config['host'];
		$this->port = $config['port'];
		$this->user = $config['user'];
		$this->passwd = $config['passwd'];
		$this->database = $config['database'];

		$this->link = @mysqli_connect( $this->host , $this->user, $this->passwd, $this->database, $this->port  )
			or view_log("Db Connect Error","Can't connect to mysql server!");		
		mysqli_query( $this->link, "set names ".$GLOBALS['config']['dcharset'].";");
	}
	
	//单件模式
	static function getInstance() {
		if( self::$mysql ==  null ){
			$class = __CLASS__;
//			self::$mysql = new $class();
			self::$mysql =   $instance = $_instance[$class] = new static();
			return $instance;
		}
		return self::$mysql;
	}

	/**
	* 执行sql语句
	* @param string $sql 
	* @return resource $query
	**/
	function query($sql){
		$query = mysqli_query($this->link, trim($sql));
		return $query;
	}

	/**
	* 得到查询的记录
	* @param string $sql
	* @return array $records
	*/
	function getRecords($sql){
		$result = $this->query($sql)
						or die( view_log( 'MySQL Query Error', mysql_error() ));

		$records = array();
		while($row = mysqli_fetch_array($result, MYSQLi_ASSOC)){
			$records[] = $row;
		}
		/*
		include_once("functions.php");
		$records = stripslashes_deep($records);     //如果结果中有被转义的字符，就反转义回来
		*/
		return $records;
	}
	
	function fetch_all($sql) {   
		$query = $this->query($sql)
				or die( view_log( 'MySQL Query Error', mysqli_error($this->link) ));
		
		$result = array();
		while( $rs = mysqli_fetch_assoc($query ) ){
			$result[] = $rs;
		} 
		
	
        return $result;   
    
    }   

	/**
	* 得到第一条查询的记录的第一个字段值
	* @param string $sql
	* @return array $records
	*/
	function getFirst( $sql ){
		$result = $this->query( $sql )
						or die( view_log( 'MySQL Query Error', mysql_error() ));
		$record = @mysql_result($result, 0);
		if( is_string($record) ){
			return $record;
		}
	}


	/**
	* 插入新数据返回id
	* @param string $sql
	* @return int insert_id
	*/
	function insertRecord($sql){
		//$result = $this->query($sql)
		//				or die( view_log( 'MySQL Query Error', "Insert data false" ));
		//if($result){
			return mysqli_insert_id($this->link);
		//}
	}

	/**
	* 更新数据
	* @param string $sql
	* @return bool
	*/
	function updateRecord($sql){
		$query = $this->query($sql)
						or die( view_log( 'MySQL Query Error', 'Update data false' ));
		if(mysqli_affected_rows($this->link) == -1){
			return false;
		} else{
			return true;
		}
	}

	/**
	* 删除数据
	* @param string $sql
	* @return bool
	*/
	function deleteRecord($sql){
		$query = $this->query($sql)
						or die( view_log( 'MySQL Query Error', 'Delete data false' ));
		if(mysqli_affected_rows($this->link) == -1){
			return false;
		} else{
			return true;
		}
	}

	/**
	* 得到查询结果集的行数
	* @param resource $result
	* @return int
	*/
	function getRows($result){
		return mysqli_num_rows($result);
	}

	/**
	* 得到查询结果集的列数(字段数)
	* @param resource $result
	* @return int
	*/
	function getColumns($result){
		return mysqli_num_fields($result);
	}


	//断开
	function __distruct(){
		mysqli_close( $this->link );
	}
}
?>
